package com.ganzi.manager.activiti.util;

import com.ganzi.manager.activiti.base.CreateModelException;
import java.lang.reflect.Method;

/**
 * 反射工具
 * */
public class ReflectUtil {
    private static String ACTIVITI_PACKAGE_NAME = "org.activiti.bpmn.model.";

    /**
     * 创建节点实例
     * */
    public static Object createObject(String className){
        Class<?> temp;
        Object objectTemp;
        try {
            temp = Class.forName(ACTIVITI_PACKAGE_NAME+className.trim());
            objectTemp = temp.newInstance();
            return objectTemp;
        } catch (InstantiationException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 调用方法
     * */
    public static void invokeMethod(String className,String methodName,Class<?> argType,Object arg,Object objectTemp){
        Class<?> temp;
        try {
            temp = Class.forName(ACTIVITI_PACKAGE_NAME + className.trim());
            Method setName = temp.getMethod(methodName,argType);
            setName.setAccessible(true);
            setName.invoke(objectTemp,arg);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CreateModelException();
        }
    }
}
